package com.ruoyi.mp.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mp.service.IMpAccountService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <h3>springcloudSaas</h3>
 * <p>数据统计</p>
 *
 * @author : 郎兴宇
 * @date : 2024-09-28 17:25
 **/
@RestController
@RequestMapping("/dataCube")
public class MpDataCubeController extends BaseController {
    @Autowired
    private WxMpService wxService;
    @Autowired
    private IMpAccountService iMpAccountService;

    /**
     * 获取累计用户数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  7天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getUserCumulate/{id}")
    @RequiresPermissions("mp:dataCube:getUserCumulate")
    @Log(title = "获取累计用户数据", businessType = BusinessType.EXPORT)
    public AjaxResult addTemplate(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getUserCumulate(beginDate, endDate));
    }

    /**
     * 获取用户增减数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  7天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getUserSummary/{id}")
    @RequiresPermissions("mp:dataCube:getUserSummary")
    @Log(title = "获取用户增减数据", businessType = BusinessType.EXPORT)
    public AjaxResult getUserSummary(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getUserSummary(beginDate, endDate));
    }

    /**
     * 获取图文群发每日数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  1天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getArticleSummary/{id}")
    @RequiresPermissions("mp:dataCube:getArticleSummary")
    @Log(title = "获取图文群发每日数据", businessType = BusinessType.EXPORT)
    public AjaxResult getArticleSummary(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getArticleSummary(beginDate, endDate));
    }

    /**
     * 获取图文群发总数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  1天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getArticleTotal/{id}")
    @RequiresPermissions("mp:dataCube:getArticleTotal")
    @Log(title = "获取图文群发总数据", businessType = BusinessType.EXPORT)
    public AjaxResult getArticleTotal(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getArticleTotal(beginDate, endDate));
    }

    /**
     * 获取图文统计数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  3天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getUserRead/{id}")
    @RequiresPermissions("mp:dataCube:getUserRead")
    @Log(title = "获取图文统计数据", businessType = BusinessType.EXPORT)
    public AjaxResult getUserRead(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getUserRead(beginDate, endDate));
    }


    /**
     *  获取图文统计分时数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  1天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getUserReadHour/{id}")
    @RequiresPermissions("mp:dataCube:getUserReadHour")
    @Log(title = " 获取图文统计分时数据", businessType = BusinessType.EXPORT)
    public AjaxResult getUserReadHour(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getUserReadHour(beginDate, endDate));
    }


    /**
     *  获取图文分享转发数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  7天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getUserShare/{id}")
    @RequiresPermissions("mp:dataCube:getUserShare")
    @Log(title = "获取图文分享转发数据", businessType = BusinessType.EXPORT)
    public AjaxResult getUserShare(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getUserShare(beginDate, endDate));
    }


    /**
     *  获取图文分享转发分时数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  1天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getUserShareHour/{id}")
    @RequiresPermissions("mp:dataCube:getUserShareHour")
    @Log(title = "获取图文分享转发分时数据", businessType = BusinessType.EXPORT)
    public AjaxResult getUserShareHour(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getUserShareHour(beginDate, endDate));
    }



    /**
     *  获取消息发送概况数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  7天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getUpstreamMsg/{id}")
    @RequiresPermissions("mp:dataCube:getUpstreamMsg")
    @Log(title = "获取消息发送概况数据", businessType = BusinessType.EXPORT)
    public AjaxResult getUpstreamMsg(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getUpstreamMsg(beginDate, endDate));
    }


    /**
     *  获取消息分送分时数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  1天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getUpstreamMsgHour/{id}")
    @RequiresPermissions("mp:dataCube:getUpstreamMsgHour")
    @Log(title = "获取消息分送分时数据", businessType = BusinessType.EXPORT)
    public AjaxResult getUpstreamMsgHour(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getUpstreamMsgHour(beginDate, endDate));
    }



    /**
     *   获取消息发送周数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  30天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getUpstreamMsgWeek/{id}")
    @RequiresPermissions("mp:dataCube:getUpstreamMsgWeek")
    @Log(title = " 获取消息发送周数据", businessType = BusinessType.EXPORT)
    public AjaxResult getUpstreamMsgWeek(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getUpstreamMsgWeek(beginDate, endDate));
    }

    /**
     *   获取消息发送月数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  30天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getUpstreamMsgMonth/{id}")
    @RequiresPermissions("mp:dataCube:getUpstreamMsgMonth")
    @Log(title = "获取消息发送月数据", businessType = BusinessType.EXPORT)
    public AjaxResult getUpstreamMsgMonth(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getUpstreamMsgMonth(beginDate, endDate));
    }


    /**
     *   获取消息发送分布数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  15天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getUpstreamMsgDist/{id}")
    @RequiresPermissions("mp:dataCube:getUpstreamMsgDist")
    @Log(title = "获取消息发送分布数据", businessType = BusinessType.EXPORT)
    public AjaxResult getUpstreamMsgDist(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getUpstreamMsgDist(beginDate, endDate));
    }

    /**
     *   获取消息发送分布周数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  30天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getUpstreamMsgDistWeek/{id}")
    @RequiresPermissions("mp:dataCube:getUpstreamMsgDistWeek")
    @Log(title = "获取消息发送分布周数据", businessType = BusinessType.EXPORT)
    public AjaxResult getUpstreamMsgDistWeek(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getUpstreamMsgDistWeek(beginDate, endDate));
    }


    /**
     *   获取消息发送分布月数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  30天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getUpstreamMsgDistMonth/{id}")
    @RequiresPermissions("mp:dataCube:getUpstreamMsgDistMonth")
    @Log(title = "获取消息发送分布月数据", businessType = BusinessType.EXPORT)
    public AjaxResult getUpstreamMsgDistMonth(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getUpstreamMsgDistMonth(beginDate, endDate));
    }


    /**
     *   获取接口分析数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  30天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getInterfaceSummary/{id}")
    @RequiresPermissions("mp:dataCube:getInterfaceSummary")
    @Log(title = "获取接口分析数据", businessType = BusinessType.EXPORT)
    public AjaxResult getInterfaceSummary(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getInterfaceSummary(beginDate, endDate));
    }


    /**
     *   获取接口分析分时数据
     *
     * @param id       小程序ID
     * @param beginDate 开始日期
     * @param endDate   结束日期  1天
     * @return 返回累计用户数据的AjaxResult对象
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getInterfaceSummaryHour/{id}")
    @RequiresPermissions("mp:dataCube:getInterfaceSummaryHour")
    @Log(title = "获取接口分析分时数据", businessType = BusinessType.EXPORT)
    public AjaxResult getInterfaceSummaryHour(@PathVariable Long id, Date beginDate, Date endDate) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        return AjaxResult.success(wxService.switchoverTo(appId).getDataCubeService().getInterfaceSummaryHour(beginDate, endDate));
    }
}


